package com.ruoyi.report_lq.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ruoyi.syrw.annotation.FIllBasicInfo;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.ruoyi.report_lq.domain.bo.ReportRhlqZfclwhljlbBsInfoBo;
import com.ruoyi.report_lq.domain.vo.ReportRhlqZfclwhljlbBsInfoVo;
import com.ruoyi.report_lq.domain.ReportRhlqZfclwhljlbBsInfo;
import com.ruoyi.report_lq.mapper.ReportRhlqZfclwhljlbBsInfoMapper;
import com.ruoyi.report_lq.service.IReportRhlqZfclwhljlbBsInfoService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 【请填写功能名称】Service业务层处理
 *
 * @author zyb
 * @date 2024-07-03
 */
@RequiredArgsConstructor
@Service
public class ReportRhlqZfclwhljlbBsInfoServiceImpl implements IReportRhlqZfclwhljlbBsInfoService {

    private final ReportRhlqZfclwhljlbBsInfoMapper baseMapper;

    private final ReportRhlqZfclwhljlbBaDetailServiceImpl detailService;

    /**
     * 查询【请填写功能名称】
     */
    @Override
    @FIllBasicInfo
    public ReportRhlqZfclwhljlbBsInfoVo queryById(Long excelId){
        ReportRhlqZfclwhljlbBsInfoVo info = baseMapper.selectVoById(excelId);
        // TODO 查询蒸发残留物含量子表数据
        info.setBaList(detailService.queryByInfoId(excelId));
        return info;
    }

    /**
     * 查询【请填写功能名称】列表
     */
    @Override
    public TableDataInfo<ReportRhlqZfclwhljlbBsInfoVo> queryPageList(ReportRhlqZfclwhljlbBsInfoBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<ReportRhlqZfclwhljlbBsInfo> lqw = buildQueryWrapper(bo);
        Page<ReportRhlqZfclwhljlbBsInfoVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询【请填写功能名称】列表
     */
    @Override
    public List<ReportRhlqZfclwhljlbBsInfoVo> queryList(ReportRhlqZfclwhljlbBsInfoBo bo) {
        LambdaQueryWrapper<ReportRhlqZfclwhljlbBsInfo> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<ReportRhlqZfclwhljlbBsInfo> buildQueryWrapper(ReportRhlqZfclwhljlbBsInfoBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<ReportRhlqZfclwhljlbBsInfo> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getTotalPage() != null, ReportRhlqZfclwhljlbBsInfo::getTotalPage, bo.getTotalPage());
        lqw.eq(bo.getPage() != null, ReportRhlqZfclwhljlbBsInfo::getPage, bo.getPage());
        lqw.eq(StringUtils.isNotBlank(bo.getJilbh()), ReportRhlqZfclwhljlbBsInfo::getJilbh, bo.getJilbh());
        lqw.eq(StringUtils.isNotBlank(bo.getJiancdwmc()), ReportRhlqZfclwhljlbBsInfo::getJiancdwmc, bo.getJiancdwmc());
        lqw.eq(StringUtils.isNotBlank(bo.getGongcbwyt()), ReportRhlqZfclwhljlbBsInfo::getGongcbwyt, bo.getGongcbwyt());
        lqw.eq(StringUtils.isNotBlank(bo.getYangpmc()), ReportRhlqZfclwhljlbBsInfo::getYangpmc, bo.getYangpmc());
        lqw.eq(StringUtils.isNotBlank(bo.getYangpbh()), ReportRhlqZfclwhljlbBsInfo::getYangpbh, bo.getYangpbh());
        lqw.eq(StringUtils.isNotBlank(bo.getYangpsl()), ReportRhlqZfclwhljlbBsInfo::getYangpsl, bo.getYangpsl());
        lqw.eq(StringUtils.isNotBlank(bo.getYangpzt()), ReportRhlqZfclwhljlbBsInfo::getYangpzt, bo.getYangpzt());
        lqw.eq(StringUtils.isNotBlank(bo.getYangpsj()), ReportRhlqZfclwhljlbBsInfo::getYangpsj, bo.getYangpsj());
        lqw.eq(StringUtils.isNotBlank(bo.getShiyjcrq()), ReportRhlqZfclwhljlbBsInfo::getShiyjcrq, bo.getShiyjcrq());
        lqw.eq(StringUtils.isNotBlank(bo.getShiytjwd()), ReportRhlqZfclwhljlbBsInfo::getShiytjwd, bo.getShiytjwd());
        lqw.eq(StringUtils.isNotBlank(bo.getShiytjsd()), ReportRhlqZfclwhljlbBsInfo::getShiytjsd, bo.getShiytjsd());
        lqw.eq(StringUtils.isNotBlank(bo.getJiancyj()), ReportRhlqZfclwhljlbBsInfo::getJiancyj, bo.getJiancyj());
        lqw.eq(StringUtils.isNotBlank(bo.getPandyj()), ReportRhlqZfclwhljlbBsInfo::getPandyj, bo.getPandyj());
        lqw.eq(StringUtils.isNotBlank(bo.getZhuyyqsbmcjbh()), ReportRhlqZfclwhljlbBsInfo::getZhuyyqsbmcjbh, bo.getZhuyyqsbmcjbh());
        lqw.eq(StringUtils.isNotBlank(bo.getFujsm()), ReportRhlqZfclwhljlbBsInfo::getFujsm, bo.getFujsm());
        lqw.eq(StringUtils.isNotBlank(bo.getShebIds()), ReportRhlqZfclwhljlbBsInfo::getShebIds, bo.getShebIds());
        lqw.eq(StringUtils.isNotBlank(bo.getQuydd()), ReportRhlqZfclwhljlbBsInfo::getQuydd, bo.getQuydd());
        lqw.eq(StringUtils.isNotBlank(bo.getRuhlqpz()), ReportRhlqZfclwhljlbBsInfo::getRuhlqpz, bo.getRuhlqpz());
        lqw.eq(StringUtils.isNotBlank(bo.getRuhlqdh()), ReportRhlqZfclwhljlbBsInfo::getRuhlqdh, bo.getRuhlqdh());
        lqw.eq(StringUtils.isNotBlank(bo.getJizlqcd()), ReportRhlqZfclwhljlbBsInfo::getJizlqcd, bo.getJizlqcd());
        lqw.eq(StringUtils.isNotBlank(bo.getJizlqzl()), ReportRhlqZfclwhljlbBsInfo::getJizlqzl, bo.getJizlqzl());
        lqw.eq(StringUtils.isNotBlank(bo.getJizlqbh()), ReportRhlqZfclwhljlbBsInfo::getJizlqbh, bo.getJizlqbh());
        return lqw;
    }

    /**
     * 新增【请填写功能名称】
     */
    @Override
    public Boolean insertByBo(ReportRhlqZfclwhljlbBsInfoBo bo) {
        ReportRhlqZfclwhljlbBsInfo add = BeanUtil.toBean(bo, ReportRhlqZfclwhljlbBsInfo.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setExcelId(add.getExcelId());
        }
        return flag;
    }

    /**
     * 修改【请填写功能名称】
     */
    @Override
    public Boolean updateByBo(ReportRhlqZfclwhljlbBsInfoBo bo) {
        boolean flag;
        ReportRhlqZfclwhljlbBsInfo update = BeanUtil.toBean(bo, ReportRhlqZfclwhljlbBsInfo.class);
        validEntityBeforeSave(update);
        flag = baseMapper.updateById(update) > 0;
        // TODO 查询蒸发残留物含量子表数据
        detailService.insertOrUpdateByClwBo(bo.getBaList(), update.getExcelId());
        return flag;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(ReportRhlqZfclwhljlbBsInfo entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除【请填写功能名称】
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
